﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BaoVeBienCuong.Playgame.Button
{
    class ButtonsPlay
    {
        private int Type;//0 là pause, 1 là speed, 2 là zoom, 3 là mưa lửa, 4 là bẫy lửa,5 là kiếm, 6 là cung, 7 là phép, 8 là đao, 9 là tướng
        private int Price;
        private Vector2 position;
        private Vector2 size;
        private Texture2D image;
        private bool isEnable;
        private bool isSelect;
        private float scale;
        public ButtonsPlay()
        {
            Type = 0;
            position = new Vector2(0, 0);
            size = new Vector2(135, 94);
            isSelect = false;
        }
        public ButtonsPlay(int type)
        {
            Type = type;
            isEnable = false;
            isSelect = false;
            scale = 0.6f;
            size = new Vector2(128, 128) * scale;
            if (Type < 3)
            {
                Price = 0;
                position = new Vector2(Type * (size.X + 5), 5);
            }
            else if (Type < 5)
            {
                Price = 0;
                position = new Vector2(10 + (Type - 3) * (size.X + 5), 470 - size.Y);
            }
            else
            {
                position = new Vector2(390 + (Type - 5) * (size.X + 5), 470 - size.Y);
                if (Type == 5) Price = 50;
                else if (Type == 6) Price = 80;
                else if (Type == 9) Price = 500;
                else Price = 100;
            }
        }
        public void Load(ContentManager content)
        {
            image = content.Load<Texture2D>("images/playScreen/playScreenButton");
            if (Type > 4 && Type < 9)
            {
                Price = GamePage.save.getPrice(Type, Price);
            }
        }
        public void Draw(SpriteBatch sprite, SpriteFont font)
        {
            if (GamePage.money >= Price) isEnable = true;
            else isEnable = false;
            //vẽ ảnh
            #region
            if (Type < 2)
            {
                if ((!isEnable) || isSelect)
                {
                    sprite.Draw(image, position, new Rectangle(128 + 256 * Type, 0, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
                else
                {
                    sprite.Draw(image, position, new Rectangle(256 * Type, 0, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
            }
            else if (Type == 2)
            {
                if ((!isEnable) || isSelect)
                {
                    sprite.Draw(image, position, new Rectangle(128, 128, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
                else
                {
                    sprite.Draw(image, position, new Rectangle(0, 128, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
            }
            else if (Type == 3 || Type == 4)
            {
                if ((!isEnable) || isSelect)
                {
                    sprite.Draw(image, position, new Rectangle(1024 - 128 - (Type == 3 ? 0 : 256), 0, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
                else
                {
                    sprite.Draw(image, position, new Rectangle(1024 - 256 - (Type == 3 ? 0 : 256), 0, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
            }
            else if (Type == 5 || Type == 6)
            {
                if ((!isEnable) || isSelect)
                {
                    sprite.Draw(image, position, new Rectangle(1024 - 128 - (Type == 6 ? 0 : 256), 256, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
                else
                {
                    sprite.Draw(image, position, new Rectangle(1024 - 256 - (Type == 6 ? 0 : 256), 256, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
            }
            else if (Type == 7 || Type == 8)
            {
                if ((!isEnable) || isSelect)
                {
                    sprite.Draw(image, position, new Rectangle(512 - 128 - (Type == 7 ? 0 : 256), 256, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
                else
                {
                    sprite.Draw(image, position, new Rectangle(256 - (Type == 7 ? 0 : 256), 256, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
            }
            else
            {
                if ((!isEnable) || isSelect)
                {
                    sprite.Draw(image, position, new Rectangle(512 - 128, 128, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
                else
                {
                    sprite.Draw(image, position, new Rectangle(256, 128, 128, 128), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
            }
            #endregion

            if (Price != 0)
            {
                sprite.DrawString(font, Price.ToString(), position + size - new Vector2(30, 20), Color.White);
            }
        }
        public bool isTouch(Vector2 posi)
        {
            if (Fire.SoLuong >= 4)
            {
                if (Type == 3 || Type == 4)
                {
                    isSelect = true;
                    isEnable = false;
                    return false;
                }
            }
            if (!isEnable)
            {
                isSelect = false;
                return false;
            }
            else
            {
                if (posi.X >= position.X && posi.X <= (position.X + size.X) && posi.Y >= position.Y && posi.Y <= (position.Y + size.Y))
                {
                    if (Type == 0)
                        if (!isSelect)
                            isSelect = true;
                        else
                            isSelect = false;
                    else
                    {
                        isSelect = true;
                    }
                    return true;
                }
                else
                {
                    if (Type != 0)
                        isSelect = false;
                    return false;
                }
            }
        }
        public void setTouch(bool x)
        {
            isSelect = x;
        }
        public void setEnable(bool a)
        {
            isEnable = a;
        }
        public int getType()
        {
            return Type;
        }
        public int getPrice()
        {
            return Price;
        }
    }
}
